<?php
class BattleNetUser{
	private $UserId;
	private $BattlenetUrl;
	private	$RaceID;
	private	$LeagueWins;
	private	$OneVOneLeague;
	private	$OneVOneLeaguewins;
	private $OneVOneRank;
	private	$TwoVTwoLeague;
	private	$TwoVTwoLeaguewins;
	private	$TwoVTwoRank;
	private	$ThreeVThreeLeague;
	private	$ThreeVThreeLeaguewins;
	private	$ThreeVThreeRank;
	private	$FourVFourLeague;
	private	$FourVFourLeaguewins;
	private	$FourVFourRank;
	private $ligue = array('silver' => 'argent', 'platinum' => 'platine', 'gold' => 'or', 'diamond' => 'diamant', 'bronze' => 'bronze', 'master' => 'master', 'grandmaster' => 'grandmaster');
	
	function __construct($BattlenetUrl, $UserId)
	{
		//echo $UserId;
		$this->BattlenetUrl = $BattlenetUrl;
		$this->UserId		= $UserId;
		$this->generateRapport();
	}
	private function generateRapport()
	{
		$result = mysql_query('select id_user, id_race, leaguewins, 1v1, 1v1leaguewins, 1v1rank, 2v2, 2v2leaguewins, 2v2rank, 3v3, 3v3leaguewins,3v3rank, 4v4, 4v4leaguewins, 4v4rank, DATEDIFF( maj, NOW( ) )  as maj from _sc2battlenet where id_user = \''.$this->UserId.'\'');
		if(mysql_num_rows($result) == 1)
		{ 
			$temp = mysql_fetch_assoc($result);
			if($temp['maj'] >= 0)
			{
				$this->OneVOneLeague       = $temp['1v1'];
				$this->TwoVTwoLeague       = $temp['2v2'];
				$this->ThreeVThreeLeague   = $temp['3v3'];
				$this->FourVFourLeague     = $temp['4v4'];
				$this->OneVOneRank         = $temp['1v1rank'];
				$this->TwoVTwoRank         = $temp['2v2rank'];
				$this->ThreeVThreeRank     = $temp['3v3rank'];
				$this->FourVFourRank       = $temp['4v4rank'];
				$this->OneVOneLeaguewins   = $temp['1v1leaguewins'];
				$this->TwoVTwoLeaguewins   = $temp['2v2leaguewins'];
				$this->ThreeVThreeLeaguewins   = $temp['3v3leaguewins'];
				$this->FourVFourLeaguewins = $temp['4v4leaguewins'];
				$this->RaceID			   = $temp['id_race'];
				$this->LeagueWins 		   = $temp['leaguewins'];
				//$this->CustomGames 		   = $temp['maj'];
			}
			else
			{
				$this->getDatas();
				//$this->CustomGames 		   = ($temp = $parser->get_custom_games()) == 0 ? null : $temp;
				$sql = 'update `_sc2battlenet` set
							id_race  ='.$this->RaceID.',
							leaguewins='.$this->LeagueWins 	.', 
							1v1=\''.$this->OneVOneLeague .'\', 
							1v1leaguewins=\''.$this->OneVOneLeaguewins.'\', 
							1v1rank=\''.$this->OneVOneRank .'\', 
							2v2=\''.$this->TwoVTwoLeague .'\', 
							2v2leaguewins=\''.$this->TwoVTwoLeaguewins.'\', 
							2v2rank=\''.$this->TwoVTwoRank .'\', 
							3v3=\''.$this->ThreeVThreeLeague .'\', 
							3v3leaguewins=\''.$this->ThreeVThreeLeaguewins.'\', 
							3v3rank=\''.$this->ThreeVThreeRank .'\', 
							4v4=\''.$this->FourVFourLeague .'\', 
							4v4leaguewins=\''.$this->FourVFourLeaguewins.'\', 
							4v4rank=\''.$this->FourVFourRank .'\', 
							maj=now()
						where id_user = \''.$this->UserId.'\'';
				mysql_query($sql);
			}
			
		}
		else
		{
			$this->getDatas();
			$sql = 'INSERT INTO `_sc2battlenet` 
						(`id_user`, `id_race`, `leaguewins`, `1v1`, `1v1leaguewins`,`1v1rank`, `2v2`, `2v2leaguewins`,`2v2rank`, `3v3`, `3v3leaguewins`,`3v3rank`, `4v4`, `4v4leaguewins`,`4v4rank`, `maj`) 
						VALUES (
						\''.$this->UserId.'\',
						'.$this->RaceID.',
						'.$this->LeagueWins 	.', 
						\''.$this->OneVOneLeague .'\', 
						\''.$this->OneVOneLeaguewins.'\', 
						\''.$this->OneVOneRank .'\', 
						\''.$this->TwoVTwoLeague .'\', 
						\''.$this->TwoVTwoLeaguewins.'\',
						\''.$this->TwoVTwoRank .'\', 
						\''.$this->ThreeVThreeLeague .'\', 
						\''.$this->ThreeVThreeLeaguewins.'\', 
						\''.$this->ThreeVThreeRank .'\', 
						\''.$this->FourVFourLeague .'\', 
						\''.$this->FourVFourLeaguewins.'\', 
						\''.$this->FourVFourRank .'\', 
						now())';
			//echo $sql;
			mysql_query($sql);
			//lancement du parsage pour l'url
		}
	}
	private function getDatas()
	{
		$parser = new BattleNetParser($this->BattlenetUrl, true);
		$this->OneVOneLeague       = (is_null($temp = $parser->get_1v1_league())) ? null : Ligue::getIdByName($this->ligue[$temp]);
		$this->TwoVTwoLeague       = (is_null($temp = $parser->get_2v2_league())) ? null : Ligue::getIdByName($this->ligue[$temp]);
		$this->ThreeVThreeLeague   = (is_null($temp = $parser->get_3v3_league())) ? null : Ligue::getIdByName($this->ligue[$temp]);
		$this->FourVFourLeague     = (is_null($temp = $parser->get_4v4_league())) ? null : Ligue::getIdByName($this->ligue[$temp]);
		$this->OneVOneLeaguewins   = ($temp = $parser->get_1v1_wins()) == 0 ? null : $temp;
		$this->TwoVTwoLeaguewins   = ($temp = $parser->get_2v2_wins()) == 0 ? null : $temp;
		$this->ThreeVThreeLeaguewins   = ($temp = $parser->get_3v3_wins()) == 0 ? null : $temp;
		$this->FourVFourLeaguewins = ($temp = $parser->get_4v4_wins()) == 0 ? null : $temp;
		$this->OneVOneRank = ($temp = $parser->get_1v1_rank()) == 0 ? 100 : $temp;
		$this->TwoVTwoRank = ($temp = $parser->get_2v2_rank()) == 0 ? 100 : $temp;
		$this->ThreeVThreeRank = ($temp = $parser->get_3v3_rank()) == 0 ? 100 : $temp;
		$this->FourVFourRank = ($temp = $parser->get_4v4_rank()) == 0 ? 100 : $temp;
		$this->RaceID			   = (is_null($temp = $parser->get_race())) ? null : Race::getIdByName($temp);
		$this->LeagueWins 		   = ($temp = $parser->get_overall_league_wins()) == 0 ? null : $temp;
	}
	public function getRace(){
		return $this->RaceID;
	}
	public function getLeagueWins(){
		return $this->LeagueWins;
	}
	public function getOneVOneLeague(){
		return $this->OneVOneLeague;
	}
	public function getOneVOneLeaguewins(){
		return $this->OneVOneLeaguewins;
	}
	public function getOneVOneRank(){
		return $this->OneVOneRank;
	}
	public function getTwoVTwoLeague(){
		return $this->TwoVTwoLeague;
	}
	public function getTwoVTwoLeaguewins(){
		return $this->TwoVTwoLeaguewins;
	}
	public function getTwoVTwoRank(){
		return $this->TwoVTwoRank;
	}
	public function getThreeVThreeLeague(){
		return $this->ThreeVThreeLeague;
	}
	public function getThreeVThreeLeaguewins(){
		return $this->ThreeVThreeLeaguewins;
	}
	public function getThreeVThreeRank(){
		return $this->ThreeVThreeRank;
	}
	public function getFourVFourLeague(){
		return $this->FourVFourLeague;
	}
	public function getFourVFourLeaguewins(){
		return $this->FourVFourLeaguewins;
	}
	public function getFourVFourRank(){
		return $this->FourVFourRank;
	}
 }
 ?>